package guda.house.biz.helper.weixin;

import com.alibaba.fastjson.JSON;
import guda.house.biz.config.WeixinConfig;
import guda.house.biz.helper.weixin.form.CheckTokenResponse;
import guda.house.biz.helper.weixin.form.RefreshTokenResponse;
import guda.house.biz.helper.weixin.form.UserInfoResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by well on 16/5/28.
 */
public class WeixinHelper {
    private final static Logger log = LoggerFactory.getLogger(WeixinHelper.class);

    public static WeixinResponseToken fetchToken(String code){
        // https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
        HttpsRequest httpsRequest = new HttpsRequest();
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WeixinConfig.appId + "&secret=" + WeixinConfig.appSecret
                + "&code=" + code + "&grant_type=authorization_code";
        String s = httpsRequest.sendGet(url);
        log.info("request:"+url+"response:"+s);
        if(s == null || s.contains("errmsg")){
            return null;
        }
        return JSON.parseObject(s, WeixinResponseToken.class);

    }

    public static CheckTokenResponse checkToken(String openid,String accessToken){
        // https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
        HttpsRequest httpsRequest = new HttpsRequest();
        String url = "https://api.weixin.qq.com/sns/auth?access_token="+accessToken+"&openid="+openid;
        String s = httpsRequest.sendGet(url);
        log.info("request:"+url+"response:"+s);
        if(s == null){
            return null;
        }
        return JSON.parseObject(s, CheckTokenResponse.class);

    }

    public static RefreshTokenResponse refreshToken(String refreshToken){
        //https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
        HttpsRequest httpsRequest = new HttpsRequest();
        String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+WeixinConfig.appId+"&grant_type=refresh_token&refresh_token=REFRESH_TOKEN"+refreshToken;
        String s = httpsRequest.sendGet(url);
        log.info("request:"+url+"response:"+s);
        if(s == null || s.contains("errmsg")){
            return null;
        }
        return JSON.parseObject(s, RefreshTokenResponse.class);
    }

    public static UserInfoResponse fetchUserInfo(String accessToken,String openid){
        //https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
        HttpsRequest httpsRequest = new HttpsRequest();
        String url = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openid+"&lang=zh_CN";
        String s = httpsRequest.sendGet(url);
        log.info("request:"+url+"response:"+s);
        if(s == null || s.contains("errmsg")){
            return null;
        }
        return JSON.parseObject(s, UserInfoResponse.class);
    }
}
